package ex.com.jm.food.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import ex.com.jm.food.dao.AdminCPSDao;
import ex.com.jm.food.entity.CompanyProfitSummary;
/**
 * 实现层
 * @author zhaojw
 *
 */
public class AdminCPSDaoImpl extends HibernateDaoSupport implements AdminCPSDao {

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List<CompanyProfitSummary> getCPSs(final int initPage,final int pageRow,
			final String name,final String s_date,final String e_date,final String sort,final String order) {
		// TODO Auto-generated method stub
		Object cpss=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="from CompanyProfitSummary cps where 1=1 ";
				if(name!=null&&!name.equals("")){
					hql += " and cps.comName like '%"+name+"%'";
				}
				if(s_date!=null&&!s_date.equals("")){
					hql +=" and cps.sDate >='"+s_date+"'";
				}
				if(e_date!=null&&!e_date.equals("")){
					hql += " and cps.sDate <= '"+e_date+"'";
				}
				if(sort!=null&&!sort.equals("")&&order!=null&&!order.equals("")){
					hql +=" order by cps."+order+" "+sort;
				}
				Query query=session.createQuery(hql).setFirstResult(initPage).setMaxResults(pageRow);
				return query.list();
			}
		});
		return (List<CompanyProfitSummary>) cpss;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public int cpsCount(final String name,final String s_date,final String e_date) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="from CompanyProfitSummary cps where 1=1 ";
				if(name!=null&&!name.equals("")){
					hql += " and cps.comName like '%"+name+"%'";
				}
				if(s_date!=null&&!s_date.equals("")){
					hql +=" and cps.sDate >='"+s_date+"'";
				}
				if(e_date!=null&&!e_date.equals("")){
					hql += " and cps.sDate <= '"+e_date+"'";
				}
				Query query=session.createQuery(hql);
				return query.list().size();
			}
		});
		return count;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<CompanyProfitSummary> CPSExcel(final String name,final String s_date,
			final String e_date) {
		// TODO Auto-generated method stub
		Object cps=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="from CompanyProfitSummary cps where 1=1 ";
				if(name!=null&&!name.equals("")){
					hql += " and cps.comName like '%"+name+"%'";
				}
				if(s_date!=null&&!s_date.equals("")){
					hql +=" and cps.sDate >='"+s_date+"'";
				}
				if(e_date!=null&&!e_date.equals("")){
					hql += " and cps.sDate <= '"+e_date+"'";
				}
				hql+=" order by sDate desc";
				Query query=session.createQuery(hql);
				return query.list();
			}
		});
		return (List<CompanyProfitSummary>) cps;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List<CompanyProfitSummary> CPSReport(final String name,final String s_date,
			final String e_date) {
		// TODO Auto-generated method stub
		Object cps=this.getHibernateTemplate().execute(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
		String hql="from CompanyProfitSummary cps where 1=1 ";
		if(name!=null&&!name.equals("")){
			hql += " and cps.comName like '%"+name+"%'";
		}
		if(s_date!=null&&!s_date.equals("")){
			hql +=" and cps.sDate >='"+s_date+"'";
		}
		if(e_date!=null&&!e_date.equals("")){
			hql += " and cps.sDate <= '"+e_date+"'";
		}
		hql+=" group by cps.comId,cps.sDate  order by sDate desc";
		Query query=session.createQuery(hql);
		return query.list();
  	}
   });
return (List<CompanyProfitSummary>) cps;
	}

}
